package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.modeling.dto.*;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.common.dto.ListResult;
import com.mysoft.gfyx.common.rpc.bigdata.BigDataClientFactoryService;
import com.mysoft.gfyx.common.rpc.bigdata.dto.GetNewHouseSaleListDto;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetNewHouseSaleRequestDto;
import com.mysoft.gfyx.marketdevelop.service.NewHouseSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mysoft.gfyx.common.rpc.bigdata.BigDataClientService;

import java.util.ArrayList;
import java.util.Map;

@Service
public class NewHouseSaleServiceImp extends AppService implements NewHouseSaleService {

    @Autowired
    BigDataClientFactoryService bigDataClientFactoryService;

    public FieldsSetting loadFields() {
        //硬编码的获取字段逻辑
        FieldsSetting fieldsSetting = new FieldsSetting();
        fieldsSetting.setIdField("master_id");
        fieldsSetting.setFields(new ArrayList<>());
        String groupName = "new_house_sale";
        fieldsSetting.getFields().add(new DataApiField("master_id", groupName, "主键id", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("presell_num", groupName, "预售证号", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("project_title", groupName, "项目名称", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("build_area", groupName, "建筑面积", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("build_name", groupName, "楼栋号", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("build_floor", groupName, "楼层", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("city_name", groupName, "城市", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("developer", groupName, "开发商", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("inner_area", groupName, "套内面积", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("project_address", groupName, "项目地址", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("region_name", groupName, "区域", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("room_state", groupName, "销售状态", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("room_use", groupName, "用途", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("room_name", groupName, "房间号", FieldType.String));
        fieldsSetting.getFields().add(new DataApiField("supplier_date", groupName, "供应日期", FieldType.Decimal));
        fieldsSetting.getFields().add(new DataApiField("trade_date", groupName, "成交日期", FieldType.String));

        // 返回
        return fieldsSetting;
    }

    public ListDataResult loadData(LoadDataParams options) {
        GetNewHouseSaleRequestDto dto = new GetNewHouseSaleRequestDto();
        dto.setPageNum(options.getPageIndex() + 1);
        dto.setPageSize(options.getPageSize());
        dto.setPresellNum(options.getOptions().getContext().get("presellNum").toString());
        dto.setOrganizingCode(options.getOptions().getContext().get("organizingCode").toString());

        BigDataClientService bigDataClientService = bigDataClientFactoryService.getBigDataClientService();
        GetNewHouseSaleListDto res = bigDataClientService.getNewHouseSaleList(dto.getPresellNum(), dto.getOrganizingCode(),
                dto.getPageNum(), dto.getPageSize());
        if (res == null || res.getData() == null || res.getData().getRows().size() == 0) {
            ListDataResult result = new ListDataResult();
            result.setTotal(0);
            result.setData(new ArrayList<>());
            return result;
        }

        ListResult<Map<String, Object>> listDataResult = new ListResult<>();
        listDataResult.setTotal(res.getData().getTotalNum());
        listDataResult.setData(res.getData().getRows());
        //处理返回数据结构
        ListDataResult result = new ListDataResult();
        result.setTotal(listDataResult.getTotal());
        result.setData(listDataResult.getData());
        return result;
    }

    @Override
    public GetNewHouseSaleListDto getNewHouseSaleList(GetNewHouseSaleRequestDto dto) {
        BigDataClientService bigDataClientService = bigDataClientFactoryService.getBigDataClientService();
        return bigDataClientService.getNewHouseSaleList(dto.getPresellNum(), dto.getOrganizingCode(), dto.getPageNum(), dto.getPageSize());
        //return res;
    }
}
